/**
 * 
 */
package org.springyu.codegenerator.service;

import java.util.List;
import java.util.Map;

import org.springyu.codegenerator.dao.Dao;
import org.springyu.codegenerator.model.Column;
import org.springyu.codegenerator.model.TabStruct;

/**
 * <p>
 * Title: 类Service.java的实现描述：
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: springyu技术有限公司 Copyright (c) 2012
 * </p>
 * <p>
 * Company: springyu技术有限公司
 * </p>
 * 
 * @author springyu 2012-9-5
 */
public class Service {

	private Dao dao;

	/**
	 * @return the dao
	 */
	public Dao getDao() {
		return dao;
	}

	/**
	 * @param dao
	 *            the dao to set
	 */
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	public List<TabStruct> queryTabStruct(String catalog, String schema,
			String tablePattern) {
		//查询表信息
		List<TabStruct> tabList = dao.getTables(catalog, schema, tablePattern);
		if (null != tabList && tabList.size() > 0) {
			for (int i=0;i<tabList.size();i++){
				TabStruct tab = tabList.get(i);
				//查询表所有列信息
				List<Column> cList= dao.getTableColumns(catalog, schema, tab.getName());
				Map<String, Object> commentsMap = dao.getColumnComments(schema,  tab.getName());
				Map<String, Object> keyMap = dao.getPrimaryKeyColumns(catalog,schema, tab.getName());
				Map<String, Object> foreignMap  = dao.getForeignKeyColumns(catalog,schema, tab.getName());
				for (int j=0;j<cList.size();j++){
					Column aCol = cList.get(j);
					/**
					 * 列名备注
					 */
					if (commentsMap.containsKey(aCol.getName())) {
						aCol.setComment((String) commentsMap.get(aCol.getName()));
					}
					/**
					 * 表主键
					 */
					if (keyMap.containsKey(aCol.getName())){
						aCol.setIsKey(Boolean.TRUE);
					}
					/**
					 * 表外键
					 */
					if (foreignMap.containsKey(aCol.getName())){
						aCol.setIsForeign(Boolean.TRUE);
					}
					tab.addColumn(aCol);
				}
			}
		}
		return tabList;
	}

}
